package mine.code.question.贪心;

import org.junit.Test;

public class 买卖股票的最佳时机 {
    @Test
    public void result() {
//		int arr[] = {7,1,5,3,6,4};
        int[] arr = {7, 1, 5, 3, 6, 4};
        System.out.println(maxProfit(arr));
    }

    public int maxProfit(int[] prices) {
        if (prices.length == 0) return 0;
        int minPrice = prices[0];
        int max = 0;
        for (int price : prices) {
            max = Math.max(max, price - minPrice);
            minPrice = Math.min(minPrice, price);
        }
        return max;
    }
}
